const app = getApp();
Page({
    data: {
        userName: '',
        password: '',
        smsCode: '',
        getSmsText: '获取验证码',
        getSmsCodeState:false,
        getSmsLoading:false,
        isLogin:0,

    },
    onLoad: function (option) {
        console.log(option)
        wx.setNavigationBarTitle({
            title: '密码重置'
        })
        this.setData({
            isLogin:option.isLogin
        })
        if(this.data.isLogin =='1'){
            let userInfo = wx.getStorageSync('userInfo');
            console.log(userInfo)
            this.setData({
                userName: userInfo.username
            });
        }
    },

    bindUserName: function (e) {
        this.setData({
            userName: e.detail.value
        });
    },
    bindSmsCode: function (e) {
        this.setData({
            smsCode: e.detail.value
        });
    },
    bindPassword: function (e) {
        this.setData({
            password: e.detail.value
        });
    },
    showTime: function (wait) {
        var that = this;
        if (wait == 0) {
            this.setData({
                getSmsText: '获取验证码',
                getSmsCodeState:false,
                getSmsLoading:false,

            })
            wait = 60;
        } else {
            this.setData({
                getSmsText:wait + "s后重试",
                getSmsCodeState:true,
                getSmsLoading:true,
            })
            wait--;
            setTimeout(function () {
                that.showTime(wait)
            }, 1000)
        }
    },
    getSmsCode: function () {
        if(this.data.getSmsCodeState==true){
            return
        } else {
            this.showTime(60);
            wx.showLoading({
                title:'请稍等'
            });
            console.log(this.data.userName)
            var that = this;
            wx.request({
                url: app.globalData.APIURL + '/vending-admin/admin/login/getSmsCode',
                data: {
                    mobile: that.data.userName,
                },
                // method:'GET',
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                    // 停止刷新
                    wx.hideLoading();
                    if(app.requestEro(res.data.success,res.data.msg)){
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'success',
                            duration: 2000
                        });
                    }

                },
                fail: function (err) {
                    console.log(err);
                    wx.hideLoading();
                }
            })
        }

    },
    resetPassword: function () {
        wx.showLoading({
            title:'请稍等'
        });
        console.log(this.data.userName,this.data.smsCode,this.data.password)
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/admin/login/reset',
            data: {
                mobile: that.data.userName,
                valid_code:that.data.smsCode,
                new_pwd:that.data.password
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                // 停止刷新
                wx.hideLoading();
                if(app.requestEro(res.data.success,res.data.msg)){
                    wx.showModal({
                        title:'密码重置成功',
                        content:'点击确定返回登录',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                wx.reLaunch({
                                    url: '/pages/login/login'
                                })
                            }
                        }
                    })
                }

            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })

    }


});